﻿namespace PlaylistCopy.Localization
{
    using System.Linq;
    using System.Reflection;

    public delegate string Localizer(string name, params object[] args);

    public static class NullLocalizer
    {
        public static Localizer Instance;

        static NullLocalizer()
        {
            Instance = (name, args) => LocalizerDelegate.NoLocalizationName(name);
        }
    }

    public static class LocalizerDelegate
    {
        internal static string NoLocalizationName(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return "--No localize key--";
            }

            return "--" + name + "--";
        }

        public static string Localize(string name, params object[] args)
        {
            if (name == null)
            {
                return null;
            }

            var resourceType = typeof (Properties.Resources);

            var localizationProp =
                resourceType.GetProperties(BindingFlags.Static | BindingFlags.Public)
                            .FirstOrDefault(prop => prop.Name == name);


            if (localizationProp == null)
            {
                return NoLocalizationName(name);
            }

            return string.Format(localizationProp.GetValue(obj: null) as string ?? NoLocalizationName(name), args);
        }  

    }
}
